package org.tangerinejunction.imstats.logs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.tangerinejunction.imstats.data.Account;
import org.tangerinejunction.imstats.data.AccountSet;
import org.tangerinejunction.imstats.data.Entity;
import org.tangerinejunction.imstats.data.IMDataset;

import prefuse.data.Table;

/**
 * This utility writes the custom Entity information to a file.
 * 
 * @author meganthorsen
 */
public class EntityWriter 
{
	public static final String ENTITY_FILE_NAME = "entities.txt";
	public static final String SPLITTER = ",";
	
	/**
	 * 
	 * @param dataset	the dataset containing the Entity data
	 */
	public static void writeEntityFile(IMDataset dataset)
	{
		File entityFile = new File(ENTITY_FILE_NAME);
		try 
		{
			BufferedWriter writer = new BufferedWriter(new FileWriter(entityFile));
			Table entityTable = dataset.getEntityTable();
			for (int i = 0; i < entityTable.getRowCount(); i++)
			{
				try
				{
					String entity = entityTable.getString(i, Entity.NAME_KEY);
					AccountSet accounts = (AccountSet)entityTable.get(i, Entity.ACCOUNTS_KEY);
					for (Account account : accounts)
					{
						writer.write(account.getName());
						writer.write(SPLITTER);
						writer.write(entity);
						writer.newLine();
						System.err.println(account.getName()+SPLITTER+entity);
					}
				}
				catch (IllegalArgumentException e)
				{
					//e.printStackTrace();
				}
			}
			writer.close();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
